package com.kun.admin.authority.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kun.admin.authority.model.RoleModel;
import com.kun.admin.authority.model.UserVo;
import com.kun.admin.authority.service.AuthorityUserService;
import com.kun.admin.authority.service.RoleService;
import com.kun.base.model.PageBean;
import com.kun.base.model.PaginationContext;
import com.kun.base.model.UserAndInfoModel;
import com.kun.base.web.model.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by likun on 2016/9/18 0018.
 */
@Controller
@RequestMapping("/admin/user")
public class AdminUserController {

    //    Logger logger = Logger.getLogger(AdminUserController.class);
    private static Logger logger = LoggerFactory.getLogger(AdminUserController.class);

    @Autowired
    private AuthorityUserService authorityUserService;

    @Autowired
    private RoleService roleService;

    /**
     * 获取用户列表,值返回json
     *
     * @return
     */
    @RequestMapping("/listData")
    @ResponseBody
    public PageBean<UserAndInfoModel> userListData(UserVo userVo) {
        PageHelper.startPage(userVo.getPageNumber(),
                userVo.getPageSize() == null ? PaginationContext.getPageSize() : userVo.getPageSize());
        Page<UserAndInfoModel> userList = authorityUserService.findUserList(userVo);
        return new PageBean<UserAndInfoModel>(userList);
    }

    @ResponseBody
    @RequestMapping(value = "/updateUserDept")
    public AjaxResult updateUserDept(Long userId, @RequestParam(value = "deptIds[]", required = false) long[] deptIds) {
        if (userId == null) {
            return new AjaxResult(false, "参数异常");
        }
        return new AjaxResult(authorityUserService.updateUserDept(userId, deptIds));
    }

    /**
     * 查找用户的部门
     *
     * @return
     */
    @RequestMapping("/findUserDept")
    @ResponseBody
    public List<Long> findUserDept(Long userId) {
        return authorityUserService.findUserDept(userId);
    }

    /**
     * 更新用户信息
     *
     * @return
     */
    @RequestMapping("/updateBaseUserInfo")
    @ResponseBody
    public AjaxResult updateBaseUserInfo(UserAndInfoModel userAndInfoModel) {
        if (userAndInfoModel == null || userAndInfoModel.getId() == null) {
            return new AjaxResult(false, "参数错误");
        }
        int row = authorityUserService.updateBaseUserInfo(userAndInfoModel);
        if (row == -1) {
            return new AjaxResult(false, "用户名已被占用");
        }
        return new AjaxResult(row > 0);
    }

    /**
     * 上传用户头像
     *
     * @return
     */
    @RequestMapping("/uploadHeadImage")
    @ResponseBody
    public AjaxResult uploadHeadImage(Long userId, @RequestParam(value = "headImage", required = false) MultipartFile file) {
        return new AjaxResult(authorityUserService.uploadHeadImage(userId, file));
    }

    /**
     * 修改用户密码
     *
     * @param userId      用户id
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @return
     */
    @RequestMapping("/repwassword")
    @ResponseBody
    public AjaxResult rePassword(Long userId, String oldPassword, String newPassword) {
        String msg = authorityUserService.updatePwd(userId, oldPassword, newPassword);
        return new AjaxResult("密码修改成功".equals(msg), msg);
    }

    /**
     * 添加用户
     *
     * @return
     */
    @RequestMapping("/addUser")
    @ResponseBody
    public AjaxResult addUser(String addUserName, String addUserPassword1) {
        String msg = authorityUserService.addUser(addUserName, addUserPassword1);
        return new AjaxResult(msg.equals("添加用户成功"), msg);
    }

    /**
     * 添加用户及其信息
     *
     * @param userInfo
     * @return
     */
    @RequestMapping("/addUserInfo")
    @ResponseBody
    public AjaxResult addUserInfo(UserAndInfoModel userInfo) {
        if (userInfo == null) {
            return new AjaxResult(false, "参数错误");
        }
        String msg = authorityUserService.addUserInfo(userInfo);
        return new AjaxResult("添加成功".equals(msg), msg);
    }

    /**
     * 给用户添加角色
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addRole")
    public AjaxResult addRole(Long userId, Long roleId) {
        if (userId == null || roleId == null) {
            logger.error("为用户添加角色时参数异常");
            return new AjaxResult(false);
        }
        boolean b = authorityUserService.addRole(userId, roleId);
        return new AjaxResult(b);
    }

    /**
     * 移除用户角色
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteRole")
    public AjaxResult deleteRole(Long userId, Long roleId) {
        if (userId == null || roleId == null) {
            logger.error("为用户移除角色时参数异常");
            return new AjaxResult(false);
        }
        boolean b = authorityUserService.deleteRole(userId, roleId);
        return new AjaxResult(b);
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @RequestMapping("/deleteUser")
    @ResponseBody
    public AjaxResult deleteUser(Long userId) {
        if (userId == null) {
            return new AjaxResult(false, "参数错误");
        }
        return new AjaxResult(authorityUserService.deleteUser(userId));
    }

    /**
     * 获取用户头像路径
     *
     * @param userId
     * @return
     */
    @RequestMapping("/findHeadImagePath")
    @ResponseBody
    public AjaxResult findHeadImagePath(Long userId) {
        if (userId == null) {
            return new AjaxResult(false, "参数错误");
        }
        String path = authorityUserService.findHeadImagePath(userId);
        return new AjaxResult(true, path);
    }
}
